package com.fary.resourcepath;

import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.util.ResourceUtils;

import java.net.URISyntaxException;

public class TestResourcePatternUtils {

    public static void main(String[] args) throws URISyntaxException {
        String url = "classpath*:";
        String url1 = "classpath:";
        String url2 = "http://www";
        System.out.println(ResourcePatternUtils.isUrl(url));
        System.out.println(ResourcePatternUtils.isUrl(url1));
        System.out.println(ResourcePatternUtils.isUrl(url2));

        System.out.println(ResourceUtils.toURI(url2).isAbsolute());


        System.out.println(determineRootDir("classpath*:/*/class/*.xml"));
    }

    protected static String determineRootDir(String location) {
        int prefixEnd = location.indexOf(':') + 1;
        int rootDirEnd = location.length();
        while (rootDirEnd > prefixEnd && isPattern(location.substring(prefixEnd, rootDirEnd))) {
            rootDirEnd = location.lastIndexOf('/', rootDirEnd - 2) + 1;
        }
        if (rootDirEnd == 0) {
            rootDirEnd = prefixEnd;
        }
        return location.substring(0, rootDirEnd);
    }

    public static boolean isPattern(String path) {
        return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
    }
}
